package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.dto.course.CourseFrontPageDTO;
import com.atguigu.eduservice.entity.vo.course.CourseFrontVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.dto.course.CourseInfoDTO;
import com.atguigu.eduservice.entity.vo.course.CourseInfoVO;
import com.atguigu.eduservice.entity.vo.course.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-06-23
 */
@Service
@Slf4j
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduSubjectService eduSubjectService;
    @Override
    public String saveCourseInfo(CourseInfoDTO courseInfoDTO) {
        //1.基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoDTO,eduCourse);
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("id", eduCourse.getSubjectId());
        EduSubject eduSubject = eduSubjectService.getOne(wrapper);
        eduCourse.setSubjectParentId(eduSubject.getParentId());
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0){
            throw new GuliException(20001,"添加课程失败");
        }
        String cid = eduCourse.getId();
        //2.课程介绍
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoDTO,eduCourseDescription);
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    @Override
    public CourseInfoVO getCourseInfo(String courseId) {
        CourseInfoVO courseInfoVO = new CourseInfoVO();
        //查询课程表、
        EduCourse course = baseMapper.selectById(courseId);
        log.info("查询课程表:{}",course);

        BeanUtils.copyProperties(course,courseInfoVO);
        //查询简介表
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(courseDescription,courseInfoVO);
        log.info("查询简介表:{}",courseInfoVO);
        return courseInfoVO;
    }

    @Override
    public void updateCourseInfo(CourseInfoDTO courseInfoDTO) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoDTO,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new GuliException(20001,"修改课程信息失败");
        }
        //修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoDTO,eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    //课程根据id查询课程确认信息
    @Override
    public CoursePublishVo getCoursePublishVo(String id) {
        //调用mapper
        return baseMapper.getCoursePublishVo(id);
    }

    @Override
    public void removeCourse(String courseId) {
        //根据课程id删除小结
        eduVideoService.removeVideoServiceCourseId(courseId);
        //根据课程id删除章节
        eduChapterService.removeChapterCourseId(courseId);
        //根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);
        //根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if (result == 0){
            throw  new GuliException(20001,"删除课程失败");
        }





    }







    //前端：课程分页查询
    @Override
    public Map<String, Object> getCourseList(Page<EduCourse> eduCoursePage, CourseFrontPageDTO courseFrontPageDTO) {
        QueryWrapper<EduCourse> eduTeacherQueryWrapper = new QueryWrapper<>();
        //条件查询
        //1.判断上传的一级id是否有值
        if(!StringUtils.isEmpty(courseFrontPageDTO.getSubjectParentId())){
            eduTeacherQueryWrapper.eq("subject_parent_id",courseFrontPageDTO.getSubjectParentId());
        }
        //2.判断上传的二级id是否有值
        if(!StringUtils.isEmpty(courseFrontPageDTO.getSubjectId())){
            eduTeacherQueryWrapper.eq("subject_id",courseFrontPageDTO.getSubjectId());
        }
        //3.是否按照时间排序
        if(!StringUtils.isEmpty(courseFrontPageDTO.getGmtCreateSort())){
            eduTeacherQueryWrapper.orderByDesc("gmt_create");
        }
        //4.是否按照销售排序
        if (!StringUtils.isEmpty(courseFrontPageDTO.getBuyCountSort())){
            eduTeacherQueryWrapper.orderByDesc("buy_count");
        }
        //5.是否按照价格排序
        if (!StringUtils.isEmpty(courseFrontPageDTO.getPriceSort())){
            eduTeacherQueryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(eduCoursePage,eduTeacherQueryWrapper);
        List<EduCourse> records = eduCoursePage.getRecords();//集合
        long current = eduCoursePage.getCurrent();//获取当前页页码
        long pages = eduCoursePage.getPages();//返回的总页数
        long size = eduCoursePage.getSize();//每页显示的记录数
        long total = eduCoursePage.getTotal();//返回的总记录数据
        boolean hasNext = eduCoursePage.hasNext();//是否有下页
        boolean hasPrevious = eduCoursePage.hasPrevious();//是否有上页
        Map<String, Object> courseMap = new HashMap<>();
        courseMap.put("items",records);
        courseMap.put("current",current);
        courseMap.put("pages",pages);
        courseMap.put("size",size);
        courseMap.put("total",total);
        courseMap.put("hasNext",hasNext);
        courseMap.put("hasPrevious",hasPrevious);
        return courseMap;
    }

    @Override
    public CourseFrontVO getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }


}
